<?
include('aux_includes.php');

session_start();
if ($_GET['restart'] == 'true') {
	session_destroy();
	session_start();
}

$html = new Strings('html/', '.html');

// INITIALIZE
if (!$_SESSION['id']) {
	$raca1 = new Anao('1');
	$jog1 = new Actor($raca1->getValue('nome'), $raca1, 'm');
	
	$_SESSION['jog1'] = $jog1; $_SESSION['jog1_vit']=0;

	$raca2 = new Humano('1');
	$jog2 = new Actor($raca2->getValue('nome'), $raca2, 'm');

	$_SESSION['jog2'] = $jog2; $_SESSION['jog2_vit']=0;
	$_SESSION['id'] = session_id();
	
	// initiative check
	$_SESSION['jog1_initiative'] = $jog1->getValue('initiative') + rand(1, 20);
	$_SESSION['jog2_initiative'] = $jog2->getValue('initiative') + rand(1, 20);
	if ($_SESSION['jog1_initiative'] > $_SESSION['jog2_initiative']) $_SESSION['ultimo_turno'] = 1;
	else $_SESSION['ultimo_turno'] = 2;

	$_SESSION['continue'] = true;
} else {
	$jog1 = $_SESSION['jog1'];
	$jog2 = $_SESSION['jog2'];
}

if ($_SESSION['ultimo_turno'] == 1) {
	$atacante = $_SESSION['jog1']; $defensor = $_SESSION['jog2'];
} else {
	$atacante = $_SESSION['jog2']; $defensor = $_SESSION['jog1'];
}

if ($_POST['action']!='') {
	if ($_SESSION['continue']) {
		include('combat.php');

		// PREPARE HTML
		$atacante->setValue("attack_roll", $attack_roll);
		$atacante->setValue("dano_final", $dano_final);
		$atacante->setValue("damage_roll", $damage_roll);
		$atacante->setValue("str_mod", $att_str_mod);
		
		$defensor->setValue("armor_class", $armor_class);
		$defensor->setValue("attack_roll", '');
		$defensor->setValue("dano_final", '');
		$defensor->setValue("damage_roll", '');
		$defensor->setValue("str_mod", '');
		
		if ($_SESSION['ultimo_turno'] == 1) {
			$_SESSION['jog1'] = $atacante; $_SESSION['jog2'] = $defensor; $_SESSION['ultimo_turno'] = 2;
		} else {
			$_SESSION['jog2'] = $atacante; $_SESSION['jog1'] = $defensor; $_SESSION['ultimo_turno'] = 1;
		}
	}
}

$atacante->setValue("action_form", '');
$defensor->setValue("action_form", $html->get('form'));

$html->set('jog1_stats', $html->mixArray ($jog1->toArray(), 'jog1_stats'));
$html->set('jog2_stats', $html->mixArray ($jog2->toArray(), 'jog2_stats'));

$html->set('jog1_vit', $_SESSION['jog1_vit']);
$html->set('jog2_vit', $_SESSION['jog2_vit']);

echo $html->mixStrings('index');
?>